package main

import (
	"context"
	"learngo/grpc/proto"
	"net"
	"log"
	"google.golang.org/grpc"
)

// 定义服务端实现约定的接口
type UserInfoService struct {}

var u  = UserInfoService{}

// 实现 interface
func (s *UserInfoService)GetUserInfo(ctx context.Context,req *proto.UserRequest) (resp *proto.UserResponse,err error) {

	name := req.Name

	if name == "rovast" {
		resp = &proto.UserResponse{
			Id: 6666,
			Name: "罗浩",
			Age: 46,
			Title: []string{"php","laravel","swoole","vue","this is rovast from china"},
		}
	}

	err = nil
	return
}


func main() {

	port := ":9666"

	l,err := net.Listen("tcp",port)

	if err != nil {
		log.Fatalf("listen error: %v\n", err)
	}

	s := grpc.NewServer()

	// 将 UserInfoService 注册到 gRPC
	// 注意第二个参数 UserInfoServiceServer 是接口类型的变量
	// 需要取地址传参
	proto.RegisterUserInfoServiceServer(s,&u)
	s.Serve(l)

}
